﻿package net.wangit.adm.role;

import java.util.Map;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.dev.component.ObjectTreeHelper;
import net.wangit.dev.component.UnitTree;
import net.wangit.framework.config.ProjectSettings;
import net.wangit.framework.expression.LabelExpression;
import net.wangit.jdbc.DB;
import net.wangit.user.privilege.PrivilegeUtil;
import net.wangit.util.Checker;
import net.wangit.view.tree.Tree;


public class PrivilegeUnitTree extends UnitTree {

	private static final String PROTECTED_GROUP = "admin";
	private static final String SCAN_ICON = "/domain/dev/o/usm_scan.png";
	private static final String EDIT_ICON = "/domain/dev/o/usm_edit.png";
	private static final String ADD_ICON = "/domain/dev/o/usm_add.png";
	private static final String ELEMENT_ICON_PATH = "/domain/dev/e/";
	private static final String APPEND_COLUMNS = ",ar,ms,me,ma";
	private static final String APPEND_LIMIT = " and (gid is null or gid!='admin')";
	private static final String SQL_ELEMENT = "select eid,n,a,c,g,o from ELEMENT where au=1 and uid=? order by a desc,g,o";
	private String domain;
	private Map privileges;
	private boolean disabled;
	private PrivilegeTreeUtil util;

	protected PrivilegeUnitTree(String domain, Map privileges,
			boolean disabled, PrivilegeTreeUtil util) {
		this.domain = domain;
		this.privileges = privileges;
		this.disabled = disabled;
		this.util = util;
		check = true;
	}

	protected String getIcon(DB db, String unitId, Row row) throws Exception {
		return ObjectTreeHelper.getUnitIcon(db, unitId, row, false);
	}

	protected String getAppendColumns() {
		return ",ar,ms,me,ma";
	}

	protected String getAppendLimit() {
		if (("adm".equals(domain) || "app".equals(domain))
				&& ProjectSettings.instance().getSuperUserOption() != 3)
			return " and (gid is null or gid!='admin')";
		else
			return "";
	}

	protected void setGroup(Tree group, String groupId, String name) {
		group.setName((new StringBuilder(String.valueOf(util.getName(name,
				groupId)))).append(" ").append(group.countLeaf()).toString());
		group.setCss("catalog");
		if (disabled)
			group.setCheckAttribute("style='display:none'");
	}

	protected void add2Parent(Tree parent, Tree object) {
		if (object.countLeaf() > 0)
			parent.add(object);
	}

	protected void setUnit(ActionContext ac, Tree object, DB db, String unitId,
			String name, Row row) throws Exception {
		String remark = LabelExpression.resolve(ac, row.get("ar"));
		if (Checker.isEmpty(remark))
			object.setName(util.getName(name, unitId));
		else
			object.setName((new StringBuilder(String.valueOf(util.getName(name,
					unitId)))).append(" (").append(remark).append(")")
					.toString());
		if ("1".equals(row.get("au")))
			addShowModes(object, row, unitId);
		addElements(ac, db, object, unitId);
		if (!Checker.isEmpty(row.get("dpid")))
			util.addDataPrivileges(db, object, "3", unitId);
		object.setCheckValue("-");
		object.setCheckAttribute("style='display:none'");
	}

	private int addShowModes(Tree unit, Row row, String unitId)
			throws Exception {
		int count = 0;
		if ("1".equals(row.get("ms"))) {
			unit.add(getUnitShowModeTree(unitId, "s", "查看",
					"/domain/dev/o/usm_scan.png"));
			count++;
		}
		if ("1".equals(row.get("me"))) {
			unit.add(getUnitShowModeTree(unitId, "e", "修改",
					"/domain/dev/o/usm_edit.png"));
			count++;
		}
		if ("1".equals(row.get("ma"))) {
			unit.add(getUnitShowModeTree(unitId, "a", "新增",
					"/domain/dev/o/usm_add.png"));
			count++;
		}
		return count;
	}

	private Tree getUnitShowModeTree(String unitId, String unitShowMode,
			String unitShowModeName, String icon) throws Exception {
		Tree tree = new Tree();
		tree.setId((new StringBuilder(String.valueOf(unitId))).append(".m.")
				.append(unitShowMode).toString());
		tree.setName(unitShowModeName);
		String privilege = PrivilegeUtil.getPrivilegeString(domain, "1",
				unitId, unitShowMode);
		tree.setCheckValue(privilege);
		tree.setIcon(icon);
		boolean checked = privileges.containsKey(privilege);
		tree.setCheckAttribute(util.getCheckedAttribute(checked));
		return tree;
	}

	private int addElements(ActionContext ac, DB db, Tree unitTree,
			String unitId) throws Exception {
		MapList elements = db
				.query(
						"select eid,n,a,c,g,o from ELEMENT where au=1 and uid=? order by a desc,g,o",
						unitId, 2);
		if (Checker.isEmpty(elements))
			return 0;
		for (int i = 0; i < elements.size(); i++) {
			Row row = elements.getRow(i);
			Tree element = new Tree();
			String elementId = row.get("eid");
			element.setId((new StringBuilder(String.valueOf(unitId))).append(
					"_").append(elementId).toString());
			String name = LabelExpression.resolve(ac, row.get("n"));
			element.setName(util.getName(name, elementId));
			String privilege = PrivilegeUtil.getPrivilegeString(domain, "2",
					unitId, elementId);
			element.setCheckValue(privilege);
			boolean checked = privileges.containsKey(privilege);
			element.setCheckAttribute(util.getCheckedAttribute(checked));
			String action = row.get("a", "0");
			String icon = (new StringBuilder("e")).append(row.get("c")).append(
					action).append("0.png").toString();
			element.setIcon((new StringBuilder("/domain/dev/e/")).append(icon)
					.toString());
			unitTree.add(element);
		}

		return elements.size();
	}
}
